package com.lx.nba.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.lx.nba.entiy.Team;

@Repository
@Transactional
public class TeamDao {
	
	@PersistenceContext
	private EntityManager entityManager ;
	
	public void saveTeams( List<Team> teams){
		
		for( Team team:teams ){
			
			entityManager.persist(team);
			
		}
		
		
	}
	
	public void saveTeam( Team team){
		
		
		entityManager.persist(team);
		
	}
	
	public List<Team> getAllTeams(){
		
		return entityManager.createNamedQuery("AllTeam").getResultList() ;
		
	}
	
	public Team getTeamById( int id ){
		
		List<Team> teams = entityManager.createNamedQuery("").getResultList();
		
		if( teams.isEmpty() ){
			
			return null ;
			
		}
		
		return teams.get(0) ;
	}

}
